package com.googlecode.bip.dao.deprecated;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.googlecode.bip.dao.BipDAO;


import cx.ath.choisnet.sql.SimpleQuery;

/**
 * 
 * @author Claude CHOISNET
 */
@Deprecated
public class UserDB extends BipDAO
{
    private static final long serialVersionUID = 1L;
    private final static transient Logger slogger = Logger.getLogger( UserDB.class );

    public UserDB() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
    *
    * @param username 
    * @param userpass 
    */
   public int createUser( 
           final String username, 
           final String userpass
           )
   {
       try {
           final String sql = "CALL add_user('"
               + username  
               + "','" 
               + userpass 
               + "')"
               ;
       
           return private_connectUser( sql, username );
           }
       catch( Exception e ) {
           slogger.error( "*** createUser: ", e );
           
           return 0;
           }
   }

    /**
     * Try to connect user.
     * <br/>
     * Initialize {@link Session} according to user identification.
     * 
     * @param username 
     * @param userpass 
     * @return userid - 0 is a non valid userid 
     * @throws SQLException 
     * @throws NamingException 
     * 
     */
    public int connectUser( 
            final String    username, 
            final String    userpass
            ) throws SQLException, NamingException
    {
        final String sql = "CALL check_user('"
                + username
                + "','"
                + userpass 
                + "')"
                ;
        
        return private_connectUser( sql, username );
    }
    
    
    private int private_connectUser( 
            final String    sql,
            final String    username
            ) throws SQLException, NamingException
    {
        final SimpleQuery query = new SimpleQuery( getDataSource() );
        
        slogger.info( "connectUser: " + sql );
        
        try {
            ResultSet   rSet        = query.executeQuery( sql );
            int         count       = 0;
            int         iduser      = 0;
            String      uname       = null;
            
            while( rSet.next() ) {
                iduser  = rSet.getInt( "iduser" );
                uname   = rSet.getString( "username" );
                count++;
            }
            
            if( count == 1 ) {
                // Extra check (security)
                if( username.equals( uname ) && ! username.isEmpty() ) {
                    
                    return iduser;
                    }
                }
            
            slogger.warn( 
                "*** connectUser: result count for '" + username 
                    + "' : " + count 
                    + " - uname: " + uname
                    );
            }
        catch( SQLException e ) {
            slogger.error( "*** connectUser: " + sql, e );
            }
        finally {
            query.quietClose();
            }

        // An error is occurred !
        return 0;
    }    

}
